#!/home/cvhadoop/python/epd/bin/python
import os
from scipy.linalg import qr
from scipy.cluster.vq import vq
import numpy as np
import struct
import math
def generate_hamatrix(row,col,hamatrix_path):
	print row,col
	file = os.path.join(hamatrix_path,"hamatrix%dr%dc.mat" % (row,col))
	fid = open(file,"w")
	fid.write(struct.pack("2i",row,col))
	hamatrix = np.random.standard_normal(row*row)
	hamatrix.shape = (row,row)
	hamatrix = np.mat(hamatrix)
	q,r = qr(hamatrix)
	q = np.array(q[0:col,:],dtype = np.float32)
	q = np.array(np.transpose(np.mat(q)))
	q.shape = (row,col)
	q.tofile(fid)
	fid.close()
def read_hamatrix(hamatrix_path):
	fid = open(hamatrix_path,"r")
	row,col = struct.unpack("2i",fid.read(8));
	q = np.fromfile(fid,dtype = np.float32);	
	q.shape = (row,col);
	print q[row-1,:]
	mul =  np.mat(q) * np.transpose(np.mat(q))
	for i in range(128):
		for j in range(18):
			if i!=j:
				if math.fabs(mul[i,j]) > 0.0001:
					print mul[i,j]
generate_hamatrix(128,64,"/home/cvhadoop/imr/hamatrix/")
